package xuli.dao.impl;

import entity.Warehouse_Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.C3P0Util;
import xuli.dao.WareHouseProductDao;

import java.sql.SQLException;
import java.util.List;

/**
 * @author xuli
 */
public class WareHouseProductDaoImpl implements WareHouseProductDao {
    private QueryRunner queryRunner = new QueryRunner(C3P0Util.getDataSource());


    @Override
    public Warehouse_Product findByIdAndModelId(String productId, Integer modelId) {
        String sql = "select * from warehouse_product where product_id=? and model_id=?";
        Warehouse_Product warehouseProduct = null;
        try {
            warehouseProduct = queryRunner.query(sql, new BeanHandler<>(Warehouse_Product.class), productId, modelId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return warehouseProduct;
    }


    @Override
    public List<Warehouse_Product> getProductAllModelType(Integer productId) {
        String sql = "select * from warehouse_product where product_id = ?";
        List<Warehouse_Product> warehouseProduct = null;
        try {
            warehouseProduct = queryRunner.query(sql, new BeanListHandler<>(Warehouse_Product.class), productId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return warehouseProduct;
    }

    @Override
    public void reduceInventory(Warehouse_Product warehouseProduct) {
        String sql = "update warehouse_product set current_cnt= ? where product_id = ? and model_id = ?";

        try {
            queryRunner.update(sql, warehouseProduct.getCurrent_cnt(), warehouseProduct.getProduct_id(), warehouseProduct.getModel_id());
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
